<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    用户名<input type="text" id="username" /> 密码<input type="password" id="userpwd" />
    <button id="register">注册</button>
    <script>
      Function.prototype.before = function (beforeFn) {
        let thisFn = this;
        return function () {
          let pass = beforeFn();
          console.log(this, 'this');
          if (pass) {
            thisFn.apply(this, arguments);
          }
          // thisFn(.(..arguments)
        };
      };
      function registerFn(event) {
        console.log('提交表单');
      }
      // registerFn = registerFn.before(function(){
      //     let username = document.getElementById('username').value;
      //     if(!username){
      //         return alert('用户名不能为空')
      //     }
      //     return true
      // })
      registerFn = registerFn.before(function () {
        let userPwd = document.getElementById('userpwd').value;
        if (!userPwd) {
          return alert('密码不能为空');
        }
        return true;
      });
      let registerBtn = document.getElementById('register');
      registerBtn.addEventListener('click', registerFn);
    </script>
  </body>
</html>
